import { ComponentsStateType } from "../store/componentsReducer";
import axios from "./ajax";

import type { ResDataType } from "./ajax";

type SearchOption = {
  keyword: string;
  isStar: boolean;
  isDeleted: boolean;
  page: number;
  pageSize: number;
};

type getQuestionListType = {
  id?: string;
  title?: string;
  componentList: ComponentsStateType;
};

export async function getQuestionService(id: string): Promise<ResDataType> {
  const url = `/api/question/${id}`;
  const data = (await axios.get(url)) as ResDataType;
  return data;
}

//创建问卷
export async function createQuestionService(): Promise<ResDataType> {
  const url = "/api/question";
  const data = (await axios.post(url)) as ResDataType;
  return data;
}

// 查询问卷列表
// Partial  意思是里面泛型的参数，可要可不要，只要符合某个都不会报错
export async function getQuestionListService(
  opt: Partial<SearchOption>
): Promise<ResDataType> {
  const url = "/api/question";
  const data = (await axios.get(url, { params: opt })) as ResDataType;
  return data;
}

// 更新单个问卷
export async function updateQuestionService(
  id: string,
  opt: { [key: string]: any }
) {
  const url = `/api/question/${id}`;
  const data = (await axios.patch(url, opt)) as ResDataType;
  return data;
}

// 复制问卷
export async function duplicateQuestionService(
  id: string
): Promise<ResDataType> {
  const url = `/api/question/duplicate/${id}`;
  const data = (await axios.post(url)) as ResDataType;
  return data;
}

// 批量彻底删除
export async function deleteQuestionService(
  ids: string[]
): Promise<ResDataType> {
  const url = "/api/question";
  const data = (await axios.delete(url, { data: ids })) as ResDataType;
  return data;
}
